package object;

import java.util.LinkedList;
import java.util.Queue;

class Node {
    public String val;
    public Node left;
    public Node right;
    public Node(String val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }

    @Override
    public String toString() {
        return "Node{" +
                "val='" + val + '\'' +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}
//这可以表示一棵树
class Tree {
    public Node root;
}
public class Test {
    //下面这个方法并不是创建二叉树的真正方法，这是模拟实现一个简单的二叉树
    //以后会学
    public static Node creatTree() {
        Node a = new Node("A");
        Node b = new Node("B");
        Node c = new Node("C");
        Node d = new Node("D");
        Node e = new Node("E");
        Node f = new Node("F");
        Node g = new Node("G");

        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        c.left = f;
        f.right = g;

        return a;
    }

    public static void prevOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val + "");
        prevOrder(root.left);
        prevOrder(root.right);
    }
    public static void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val + "");
        inOrder(root.right);
    }
    public static void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val + "");
    }
    public static void levelOrder(Node root) {
        if (root == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.println(cur.val + "");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    //下面这种方法求节点个数看似可以，但在OJ题却有问题
    //OJ题中一般都需要多组输入，而size被static修饰,导致这个size会一直累加
//    private static int size;
//    public static int size(Node root) {
//        if(root == null) {
//            return 0;
//        }
//        size++;
//        size(root.left);
//        size(root.right);
//        return size;
//    }

    public static int size(Node root) {
        if (root == null) {
            return 0;
        }
//        int rootNode = 1;
//        int leftNode = size(root.left);
//        int rightNode = size(root.right);
//        return rootNode + leftNode + rightNode;
        return size(root.left) + size(root.right) + 1;
    }

    public static int getLeafNodeCount(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int leftLeafNode = getLeafNodeCount(root.left);
        int rightLeafNode = getLeafNodeCount(root.right);
        return leftLeafNode + rightLeafNode;
    }

    public static int getLevelNodeCount(Node root,int k) {
        if (root == null || k < 1) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getLevelNodeCount(root.left,k - 1) + getLevelNodeCount(root.right,k -1);

    }

    public static int getHeight(Node root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        //return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
        return Math.max(leftHeight,rightHeight) +1;
    }



    public static Node find(Node root,String val) {
        if (root == null) {
            return null;
        }
        if (root.val.equals(val)) {
            return root;
        }
        Node leftResult = find(root.left,val);
        if (leftResult != null) {
            return leftResult;
        }
        Node rightResult = find(root.right,val);
        if (rightResult != null) {
            return rightResult;
        }
        return null;

    }

    public static boolean isCompleteTree(Node root) {
        if (root == null) {
            return true;
        }
        boolean isFirst = true;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            if (isFirst) {
                if (cur.left == null && cur.right == null) {
                    isFirst = false;
                } else if (cur.left != null && cur.right == null) {
                    isFirst = false;
                    queue.offer(cur.left);
                } else if (cur.left == null && cur.right != null) {
                    return false;
                } else {
                    queue.offer(cur.left);
                    queue.offer(cur.right);
                }
            } else {
                if (cur.left != null || cur.right != null) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        //这也可以表示一棵树
        //Node root = new Node("A");
        //把tree实例化表示一棵树
        //Tree tree = new Tree();

        Node root = creatTree();
        //prevOrder(root);
        //inOrder(root);
        //postOrder(root);
        //levelOrder(root);
        //System.out.println(size(root));
        //System.out.println(getLeafNodeCount(root));
        //System.out.println(getLevelNodeCount(root,3));
        //System.out.println(getHeight(root));
        //System.out.println(find(root,"G"));
        System.out.println(isCompleteTree(root));
    }
}